/**
 * 
 */
package fr.gfi.agraf.service.mapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.util.CollectionUtils;

import fr.gfi.agraf.persistence.entity.Activite;
import fr.gfi.agraf.persistence.entity.Tache;
import fr.gfi.agraf.vue.bean.ActiviteDTO;
import fr.gfi.agraf.vue.bean.PerimetreDTO;

/**
 * @author Fw
 * 
 */
public class ActiviteMapper {

	/**
	 * Constructeur
	 */
	private ActiviteMapper() {
		// Empty constructeur
	}

	/**
	 * Convertie l'activite en DTO
	 * 
	 * @param activite
	 * @return
	 */
	public static ActiviteDTO getDtoByEntity(final Activite activite, final PerimetreDTO perimetreDTO) {
		final ActiviteDTO activiteDTO = new ActiviteDTO();
		activiteDTO.setDateCreation(activite.getDateCreation());
		activiteDTO.setDateDebut(activite.getDateDebut());
		activiteDTO.setDateFin(activite.getDateFin());
		activiteDTO.setEtat(EtatMapper.entityToDto(activite.getEtat()));
		activiteDTO.setId(activite.getId());
		activiteDTO.setLibelle(activite.getLibelle());
		if (perimetreDTO == null) {
			activiteDTO.setPerimetre(PerimetreMapper.entityToDto(activite.getPerimetre()));
		} else {
			activiteDTO.setPerimetre(perimetreDTO);
		}
		activiteDTO.setRemarque(activite.getRemarque());
		final Set<Tache> tacheSet = activite.getTacheSet();
		activiteDTO.setTacheLiees(!CollectionUtils.isEmpty(tacheSet));
		final List<Tache> tacheList = new ArrayList<Tache>(tacheSet);
		activiteDTO.getTacheList().addAll(TacheMapper.entityToDTOList(tacheList, activiteDTO));
		return activiteDTO;
	}

	/**
	 * @param listeActivite
	 * @return
	 */
	public static List<ActiviteDTO> getListeDTO(final List<Activite> listeActivite,final PerimetreDTO perimetre) {
		final List<ActiviteDTO> output = new ArrayList<ActiviteDTO>();
		if (listeActivite != null) {
			for (final Activite activite : listeActivite) {
				output.add(ActiviteMapper.getDtoByEntity(activite,perimetre));
			}
		}
		return output;
	}

}
